<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="小程序openid" prop="agentOpenId">
        <el-input
          v-model="queryParams.agentOpenId"
          placeholder="请输入小程序openid"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="代理人名字" prop="agentName">
        <el-input
          v-model="queryParams.agentName"
          placeholder="请输入代理人名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="代理人地址" prop="agentAddress">
        <el-input
          v-model="queryParams.agentAddress"
          placeholder="请输入代理人地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="代理人电话" prop="agentPhone">
        <el-input
          v-model="queryParams.agentPhone"
          placeholder="请输入代理人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="代理人身份证" prop="agentIdentity">
        <el-input
          v-model="queryParams.agentIdentity"
          placeholder="请输入代理人身份证"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="绑定状态" prop="bindState">
        <el-select v-model="queryParams.bindState" placeholder="请选择绑定状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="冻结状态" prop="freezeState">
        <el-select v-model="queryParams.freezeState" placeholder="请选择冻结状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="提现类型" prop="cashoutType">
        <el-select v-model="queryParams.cashoutType" placeholder="请选择提现类型" clearable>
          <el-option
            v-for="dict in dict.type.xw_cashout_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['washer:agent:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['washer:agent:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['washer:agent:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['washer:agent:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="agentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="代理人ID" align="center" prop="agentId" />
      <el-table-column label="小程序openid" align="center" prop="agentOpenId" />
      <el-table-column label="代理人名字" align="center" prop="agentName" />
      <el-table-column label="代理人地址" align="center" prop="agentAddress" />
      <el-table-column label="代理人电话" align="center" prop="agentPhone" />
      <el-table-column label="代理人身份证" align="center" prop="agentIdentity" />
      <el-table-column label="代理人售卖分成比例（%）" align="center" prop="agentPercent" />
      <el-table-column label="代理人出租分成比例（%）" align="center" prop="agentRentPercent" />
      <el-table-column label="终端售卖分成比例（%）" align="center" prop="terminalPercent" />
      <el-table-column label="终端出租分成比例（%）" align="center" prop="terminalRentPercent" />
      <el-table-column label="提现类型" align="center" prop="cashoutType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.xw_cashout_type" :value="scope.row.cashoutType"/>
        </template>
      </el-table-column>
      <el-table-column label="商户号" align="center" prop="agentMerchantCode" />
      <el-table-column label="商户名称" align="center" prop="agentMerchantName" />
      <el-table-column label="绑定状态" align="center" prop="bindState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.bindState"/>
        </template>
      </el-table-column>
      <el-table-column label="冻结状态" align="center" prop="freezeState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.freezeState"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['washer:agent:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['washer:agent:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改代理商对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="200px">
        <el-form-item label="小程序openid" prop="agentOpenId">
          <el-input readonly v-model="form.agentOpenId" placeholder="自动生成" />
        </el-form-item>
        <el-form-item label="代理人名字" prop="agentName">
          <el-input v-model="form.agentName" placeholder="请输入代理人名字" />
        </el-form-item>
        <el-form-item label="代理人地址" prop="agentAddress">
          <el-input v-model="form.agentAddress" placeholder="请输入代理人地址" />
        </el-form-item>
        <el-form-item label="代理人电话" prop="agentPhone">
          <el-input v-model="form.agentPhone" placeholder="请输入代理人电话" />
        </el-form-item>
        <el-form-item label="代理人身份证" prop="agentIdentity">
          <el-input v-model="form.agentIdentity" placeholder="请输入代理人身份证" />
        </el-form-item>
        <el-form-item label="代理人售卖分成比例(%)" prop="agentPercent">
          <el-input v-model="form.agentPercent" placeholder="请输入代理人分成比例" />
        </el-form-item>
        <el-form-item label="代理人租用分成比例(%)" prop="agentRentPercent">
          <el-input v-model="form.agentRentPercent" placeholder="请输入代理人分成比例" />
        </el-form-item>
        <el-form-item label="终端售卖分成比例(%)" prop="terminalPercent">
          <el-input v-model="form.terminalPercent" placeholder="请输入终端分成比例" />
        </el-form-item>
        <el-form-item label="终端租用分成比例(%)" prop="terminalRentPercent">
          <el-input v-model="form.terminalRentPercent" placeholder="请输入终端分成比例" />
        </el-form-item>
        <el-form-item label="绑定状态" prop="bindState">
          <el-select v-model="form.bindState" placeholder="请选择绑定状态">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="提现类型" prop="cashoutType">
          <el-select v-model="form.cashoutType" placeholder="请选择提现类型">
            <el-option
              v-for="dict in dict.type.xw_cashout_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商户号" prop="agentMerchantCode">
          <el-input v-model="form.agentMerchantCode" placeholder="请输入商户号" />
        </el-form-item>
        <el-form-item label="商户名称" prop="agentMerchantName">
          <el-input v-model="form.agentMerchantName" placeholder="请输入商户名称" />
        </el-form-item>
        <el-form-item label="冻结状态" prop="freezeState">
          <el-select v-model="form.freezeState" placeholder="请选择冻结状态">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAgent, getAgent, delAgent, addAgent, updateAgent } from "@/api/washer/agent";

export default {
  name: "Agent",
  dicts: ['sys_yes_no','xw_cashout_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 代理商表格数据
      agentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        agentOpenId: null,
        agentName: null,
        agentAddress: null,
        agentPhone: null,
        agentIdentity: null,
        bindState: null,
        freezeState: null,
        activationCode: null,
        cashoutType:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        agentName: [
          { required: true, message: "代理人名字不能为空", trigger: "blur" }
        ],
        agentPhone: [
          { required: true, message: "代理人电话不能为空", trigger: "blur" }
        ],
        agentPercent: [
          { required: true, message: "代理人分成比例不能为空", trigger: "blur" }
        ],
        terminalPercent: [
          { required: true, message: "终端分成比例不能为空", trigger: "blur" }
        ],
        bindState: [
          { required: true, message: "绑定状态不能为空", trigger: "change" }
        ],
        freezeState: [
          { required: true, message: "冻结状态不能为空", trigger: "change" }
        ],
        hirePrimePrice: [
          { required: true, message: "租用每小时原价不能为空", trigger: "blur" }
        ],
        hireSpecialPrice: [
          { required: true, message: "租用每小时优惠价不能为空", trigger: "blur" }
        ],
        dailyTopAmount: [
          { required: true, message: "每日租金封顶金额不能为空", trigger: "blur" }
        ],
        agentRentPercent: [
          { required: true, message: "代理人租用分成比例不能为空", trigger: "blur" }
        ],
        terminalRentPercent: [
          { required: true, message: "终端租用分成比例不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询代理商列表 */
    getList() {
      this.loading = true;
      listAgent(this.queryParams).then(response => {
        this.agentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        agentId: null,
        agentOpenId: null,
        agentName: null,
        agentAddress: null,
        agentPhone: null,
        agentIdentity: null,
        agentPercent: null,
        terminalPercent: null,
        bindState: null,
        freezeState: null,
        activationCode: null,
        hirePrimePrice: null,
        hireSpecialPrice: null,
        dailyTopAmount: null,
        createBy: null,
        createdTime: null,
        updateBy: null,
        updatedTime: null,
        agentMerchantCode:null,
        agentMerchantName:null,
        cashoutType:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.agentId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加代理商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const agentId = row.agentId || this.ids
      getAgent(agentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改代理商";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.agentId != null) {
            updateAgent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAgent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const agentIds = row.agentId || this.ids;
      this.$modal.confirm('是否确认删除代理商编号为"' + agentIds + '"的数据项？').then(function() {
        return delAgent(agentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('washer/agent/export', {
        ...this.queryParams
      }, `agent_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
